home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / findgap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  4.2 KB  |  158 lines  |  [TEXT/R*ch]

  1. #include "comment.header"
  2. /* */
  3. extern unsigned char p[19][19], l[19][19];
  4. extern int MAXX, MAXY;
  5. extern int currentStone, opposingStone;
  6. extern long int rd;
  7.  
  8. extern int fiot(int , int , int );
  9. extern int fioe(int , int );
  10. extern void countlib(int , int , int );
  11. static int tdif(int , int );
  12. static int u[19][19];
  13. extern void uRandom(long int*);
  14.  
  15. #define FMAX 10
  16.  
  17. /* a poor attempt at finding the boundries between possible territories */
  18. /* genfield generates a field strength */
  19. /* findfmove returns a move on the boundry */
  20. /*     1  i--    2    field directions
  21.  *      j-- j++
  22.  *     3  i++    4
  23.  */
  24.  
  25. static void field1(int i,int j,int n,int dir) 
  26. {
  27.     u[i][j] += n;
  28.     if (n > 0) --n;
  29.     if (n < 0) ++n;
  30.     if (n == 0) return;
  31.     if (i > 0 && dir < 3 && p[i-1][j] == 0) field1(i-1,j,n,dir);
  32.     if (i < MAXX-1 && dir > 2 && p[i+1][j] == 0) field1(i+1,j,n,dir);
  33.     if (j > 0 && (dir % 1) && p[i][j-1] == 0) field1(i,j-1,n,dir);
  34.     if (j < MAXY-1 && !(dir % 1) && p[i][j+1] == 0) field1(i,j+1,n,dir);
  35. }
  36.  
  37. int fieldval(int i,int j) { return(FMAX - u[i][j]); }
  38.  
  39. void genfield()
  40. {
  41.   int i,j;
  42.   int c1,o1,n,m,s;
  43.   long int max;
  44.   int ave;
  45.  
  46.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) u[i][j] = 0;
  47.  
  48.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) {
  49.     if (p[i][j] == currentStone) {
  50.       field1(i,j,6,1);
  51.       field1(i,j,6,3);
  52.       field1(i,j,6,2);
  53.       field1(i,j,6,4);
  54.     }
  55.     if (p[i][j] == opposingStone) {
  56.       field1(i,j,-6,1);
  57.       field1(i,j,-6,3);
  58.       field1(i,j,-6,2);
  59.       field1(i,j,-6,4);
  60.     }
  61.   }
  62.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) if (p[i][j] != 0) u[i][j] = 0;
  63.   max = 0;
  64.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) max += u[i][j];
  65.   ave = max / (MAXX * MAXY);
  66.  
  67.   max = -100;
  68.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) {
  69.     if (u[i][j] < ave) u[i][j] = ave - u[i][j];
  70.     else u[i][j] = u[i][j] - ave;
  71.     if (u[i][j] > max) max = u[i][j];
  72.   }
  73.   if (max > 0) for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++)
  74.     u[i][j] = FMAX - (FMAX * u[i][j])/max;
  75.   for (i=0;i<MAXX;i++) {
  76.     if (u[i][0] > 1) u[i][0] /= 2;
  77.     if (u[i][MAXY-1] > 1) u[i][MAXY-1] /= 2;
  78.     if (u[0][i] > 1) u[0][i] /= 2;
  79.     if (u[MAXX-1][i] > 1) u[MAXX-1][i] /= 2;
  80.   }
  81.   /* corner defense */
  82.   s = (MAXX > 10) ? 5 : 1 + MAXX/2;c1 = 0; o1 = 0;
  83.   c1 = 0; o1 = 0;
  84.   for (i=0;i<s;i++) for (j=0;j<s;j++) {
  85.     if (p[i][j] == currentStone) c1++;
  86.     if (p[i][j] == opposingStone) o1++;
  87.   }
  88.   if (c1+1 >= o1 && o1*2 >= c1)
  89.     for (i=1;i<s;i++) for (j=1;j<s;j++) u[i][j] += FMAX * 20;
  90.   c1 = 0; o1 = 0;
  91.   for (i=0;i<s;i++) for (j=MAXY-1;j>=MAXY-s;j--) {
  92.     if (p[i][j] == currentStone) c1++;
  93.     if (p[i][j] == opposingStone) o1++;
  94.   }
  95.   if (c1+1 >= o1 && o1*2 >= c1)
  96.     for (i=1;i<s;i++) for (j=MAXY-2;j>=MAXY-s;j--) u[i][j] += FMAX * 20;
  97.   c1 = 0; o1 = 0;
  98.   for (i=MAXX-1;i>=MAXX-s;i--) for (j=0;j<s;j++) {
  99.     if (p[i][j] == currentStone) c1++;
  100.     if (p[i][j] == opposingStone) o1++;
  101.   }
  102.   if (c1+1 >= o1 && o1*2 >= c1)
  103.     for (i=MAXX-2;i>=MAXX-s;i--) for (j=1;j<s;j++) u[i][j] += FMAX * 20;
  104.   c1 = 0; o1 = 0;
  105.   for (i=MAXX-1;i>=MAXX-s;i--) for (j=MAXY-1;j>=MAXY-s;j--) {
  106.       if (p[i][j] == currentStone) c1++;
  107.       if (p[i][j] == opposingStone) o1++;
  108.     }
  109.   if (c1+1 >= o1 && o1*2 >= c1)
  110.     for (i=MAXX-2;i>=MAXX-s;i--)
  111.       for (j=MAXY-2;j>=MAXY-s;j--) u[i][j] += FMAX * 20;
  112.   
  113.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) if (p[i][j] != 0) u[i][j] = 0;
  114. #ifdef debug
  115.   for (i = 0; i < MAXX; i++) {
  116.       for (j = 0; j < MAXY; j++) printf("%2d ",u[i][j]);
  117.       putchar(';');
  118.       putchar('\n'); }
  119.   putchar('\n');
  120. #endif
  121. }
  122.  
  123. extern int lib;
  124.       
  125. int findfmove(int *x, int *y)
  126. {
  127.   int i,j;
  128.   int max, ti,tj;
  129.  
  130.   max = -100; ti = -1; tj = -1;
  131.   for (i=0;i<MAXX;i++) for (j=0;j<MAXY;j++) {
  132.  
  133.       /* avoid illegal move, liberty one or suicide, fill in own eye */
  134.       if (p[i][j] != 0) continue;
  135.       if (u[i][j] <= 0 || u[i][j] < max - 1) continue;
  136.       if (fiot(i,j,3)) continue;
  137.       if (fioe(i,j)) continue;
  138.       lib = 0;
  139.       countlib(i, j, currentStone);
  140.       if (lib < 2) continue;
  141.  
  142.       if (u[i][j] > max) {
  143.       max = u[i][j]; ti = i; tj = j;
  144.       } else if (u[i][j] >= max - 1) {
  145.           uRandom(&rd);
  146.       if (rd%1) {
  147.         max = u[i][j]; ti = i; tj = j;
  148.       }
  149.       }
  150.   }
  151.   *x = ti; *y = tj;
  152.   if (max > 0) return(max);
  153.   else return(0);
  154. }
  155.  
  156. /* */
  157.  
  158.